<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Gallery Controller.
 * 
 * @class     Gallery
 * @author    Iqbal
 * @version   1.0.0
 * @copyright Copyright (c) 2014 IPPI (http://www.ippi.or.id)
 */
class gallery extends CI_Controller {
    /**
	 * Constructor.
	 */
    function __construct() {
        parent::__construct();
        
        // Load model
        $this->load->model('model_gallery', 'm_gallery', TRUE);
    }
    
    /**
	 * Get Image Gallery Data.
     * 
     * @param[in] int $offset Offset of Image data
     * 
     * @return AJAX String
	 */
    function imagedata($offset=0){
        // Set variables
        $limit          = get_option('gallery_image_limit');
        
        // Get products data
        $images         = $this->m_gallery->get_all_images_data($limit, $offset);
        $image          = '';
        $more           = '';
        
        // Check response of get product data
        if( !empty($images) ){
            $total      = ippi_get_last_found_rows();
            
            foreach($images as $row){
                $date       = substr($row->datecreated,0,10);
                $date       = str_replace("-","",$date);
                
                $datetext   = strtotime($row->datecreated);
                $datetext   = date("F d, Y",$datetext);
                
                $img        = ( !empty($row->path) && file_exists($row->path) ? site_url($row->path) : site_url('content/media/gallery/image/thumb/noimage.jpg') );
                $image     .= '
                <li>
					<div class="thumbnail">
						<div class="media-wrap">
							<div class="media-inner">
								<div class="border-transparent">
									<div class="overlay-hover">
										<div class="overlay-content">
											<a href="'.$img.'" rel="image_gallery" class="glyphicon icon-show image_gallery" title="'.$row->title.'"></a>
										</div>
									</div>
								</div>
								<img src="'.$img.'" alt="'.$row->title.'" />
							</div>
						</div>
						<div class="caption">
							<div class="caption-inner">
								<a href="'.$img.'" rel="image_gallery_ttl" class="caption-title h5 image_gallery_ttl" title="'.$row->title.'">'.$row->title.'</a>
							</div>
						</div>
					</div>
				</li>';
            }
            
            $more           = '
            <div class="show-more">
				<div class="show-more-inner text-center has-arrow-bottom">
					<div class="loading-wrap"><div class="loading"></div></div>
					<a href="' . base_url("gallery/imagedata") . '" id="image_showmore" class="btn-showmore btn btn-primary btn-sm" '. ($total < $limit ? 'disabled="disabled"' : '' ) .'>Show more...</a>
				</div>
			</div>';
            
            // Set Offset
            $offset         = $offset + $limit;
        }
        
        // Set JSON data
        $data = array(
            'images'        => $image,
            'more'          => $more,
            'pagination'    => base_url() . 'gallery/imagedata/' . $offset
        );
        
        // JSON encode data
        die(json_encode($data));
    }
    
    /**
	 * Get Video Gallery by ID function.
     * 
     * @param[in] int $id Video Gallery ID
     * 
     * @return AJAX String
	 */
    function video($id){
        // Set Video Gallery ID
        if( !$id || empty($id) ) return false;
        
        // Set video variable
        $video      = '';
        
        // Get video daya
        $video_data = $this->m_gallery->get_videodata($id);
        
        // Check response of get video data
        if( !empty($video_data) ){
            $poster = ( !empty($video_data->thumb) && file_exists($video_data->thumb) ? site_url($video_data->thumb) : site_url('content/media/gallery/video/thumb/novideo.jpg') );
            $source = ( !empty($video_data->path) && file_exists($video_data->path) ? TRUE : FALSE );
            
            $video .= '
            <div class="quote" style="margin-bottom: 20px;">
                <blockquote style="margin-bottom: 10px;">
                    <h5 style="margin: 10px 0 10px 0 !important; line-height: 28px;">'.$video_data->title.'
                        <div class="pull-right clearfix">
        					<div class="control-nav margin-0"><a href="#" class="btn-close video_close"></a></div>
        				</div>
                    </h5>
                </blockquote>
            </div>
            <div class="videowrapper">
                <div class="videocontent">';
                    if( $source ){
                        $video .= '
                        <video id="video_det" class="video-js vjs-default-skin vjs-big-play-centered" width="auto" height="auto" controls="controls" preload="none" poster="' . $poster . '" data-setup="{}">
                            <source src="' . $video_data->path . '" type="video/mp4" />
                        </video>';
                    }else{
                        $video .= '
                        <div class="alert alert-info"><strong>Caution : </strong>Video doesn\'t exist</div>
                        <img src="content/media/gallery/video/thumb/novideo.jpg" style="width: 100%; height: auto;" />';
                    }
                $video .= '
                </div>
            </div>';
        }
        
        // Set JSON data
        $data = array(
            'video'     => $video,
            'source'    => $source
        );
        
        // JSON encode data
        die(json_encode($data));
    }
}

/* End of file gallery.php */
/* Location: ./application/controllers/gallery.php */